import { Body, Controller, Get, Post, SetMetadata, UseGuards } from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger'
import { AuthGuard as RoleAuthGuard } from 'src/guard/auth.guard'
import { AuthGuard } from '@nestjs/passport'
import { User } from 'src/interface/user.interface'
import { UserService } from './user.service'

@Controller('user')
@ApiTags('用户模块')
@ApiBearerAuth('jwt')
export class UserController {
    constructor(private userService: UserService) { }

    @Post('regist')
    @ApiOperation({
        summary: '用户注册'
    })
    async registUser(@Body() userDto: User) {
        return await this.userService.regist(userDto)
    }

    @Post('alter')
    @ApiOperation({
        summary: '用户修改'
    })
    async alterUser(@Body() userDto: User) {
        return await this.userService.alter(userDto)
    }

    @Get('hello')
    @SetMetadata('roles', ['admin']) // 设置元数据，表示这个接口只有 admin 角色能用，在守卫里处理逻辑
    @UseGuards(RoleAuthGuard)
    hello() {
        return {
            code: 0,
            msg: 'hello world'
        }
    }

    @Get('helloWithJwt')
    @UseGuards(AuthGuard('jwt'))
    helloWithJwt() {
        return 'hello world'
    }

    @Get('testRedis')
    testRedis() {
        return this.userService.testRedis()
    }
}
